package com.ewanghuiju.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.util.NoDoubleClickListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyRecycleView extends LinearLayout {
    RecyclerView recyclerView;
    public SmartRefreshLayout freshlayout;
    private OnRefreshLoadMoreLintenrer loadMoreLintenrer;
    private ImageView iv_no_data;
    private View error_net_ll, noda_ll;
    private TextView tv_no_data;
    private Button btn_error;
    private String no_data_text = "暂无数据";
    private boolean autoRefresh = false;
    private boolean always_not_use = false;

    public MyRecycleView(Context context) {
        super(context);
    }

    public MyRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.myrecycleview, this, true);
        initView();
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    private void initView() {
        recyclerView = findViewById(R.id.myrecycleviewid);
        freshlayout = findViewById(R.id.smartRefreshLayout);
        error_net_ll = findViewById(R.id.error_net_ll);
        noda_ll = findViewById(R.id.noda_ll);
        tv_no_data = findViewById(R.id.tv_no_data);
        iv_no_data = findViewById(R.id.iv_no_data);
        btn_error = findViewById(R.id.btn_error);
        initRefresh();
        initEvent();
    }

    private void initRefresh() {

        freshlayout.setEnableLoadMore(true);//是否启用上拉加载功能
        freshlayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能

    }

    public void disableAll() {
        freshlayout.setEnableLoadMore(false);
        freshlayout.setEnableRefresh(false);
    }

    private void initEvent() {
        btn_error.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                reset();
                if (loadMoreLintenrer != null) {
                    loadMoreLintenrer.onRefresh();
                }
            }
        });
    }


    public void setAutoRefresh(boolean autoRefresh) {
        this.autoRefresh = autoRefresh;
        if (this.autoRefresh) {
            this.autoRefresh = false;
            freshlayout.autoRefresh();
        }
    }

    public void setOnRefreshLoadLintener(final OnRefreshLoadMoreLintenrer loadMoreLintenrer) {
        this.loadMoreLintenrer = loadMoreLintenrer;
        freshlayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (loadMoreLintenrer != null) {
                    loadMoreLintenrer.onLoadMore();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                reset();
                if (loadMoreLintenrer != null) {
                    loadMoreLintenrer.onRefresh();
                }
            }
        });

    }

    public void setLoadMoreLintenrer(OnRefreshLoadMoreLintenrer loadMoreLintenrer) {
        this.loadMoreLintenrer = loadMoreLintenrer;
    }

    public void finishLoadMore() {
        finishLoadMore(false);
    }

    public void finishLoadMore(boolean is_no_more) {
        if (is_no_more) {
            freshlayout.finishLoadMoreWithNoMoreData();
        } else {
            freshlayout.finishLoadMore();
        }


    }

    public void finishRefreshs() {
        freshlayout.finishRefresh();
    }

    public void setEnableRefresh(boolean b) {
        if (!always_not_use) {
            freshlayout.setEnableRefresh(b);
        }

    }

    public void setEnableLoadMores(boolean b) {
        if (!always_not_use) {
            freshlayout.setEnableLoadMore(b);
        }

    }

    public void setEnableOverScrollDrags(boolean b) {
        freshlayout.setEnableOverScrollDrag(b);
    }

    public void setNo_data_text(String no_data_text) {
        setNo_data_text(-1, no_data_text);
    }

    public void setNo_data_text(int icon, String no_data_text) {
        this.no_data_text = no_data_text;
        tv_no_data.setText(no_data_text);
        if (icon != -1) {
            iv_no_data.setImageResource(icon);
        }
    }

    public void showError() {
        recyclerView.setVisibility(GONE);
        error_net_ll.setVisibility(VISIBLE);
    }

    public void showNoData() {
        recyclerView.setVisibility(GONE);
        noda_ll.setVisibility(VISIBLE);
    }

    public void reset() {
        noda_ll.setVisibility(GONE);
        error_net_ll.setVisibility(GONE);
        recyclerView.setVisibility(VISIBLE);
        freshlayout.setNoMoreData(false);
    }

    public interface OnRefreshLoadMoreLintenrer {
        void onRefresh();

        void onLoadMore();
    }
}
